package com.example.smartcity.ui

import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.graphics.luminance
import androidx.core.view.get
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.example.smartcity.MainActivity
import com.example.smartcity.R
import com.example.smartcity.SmartCityApplication.Companion.context
import com.example.smartcity.databinding.ActivityGuideBinding
import com.example.smartcity.logic.utils.BaseActivity

/**
 * 引导页
 */
class GuideActivity : BaseActivity() ,View.OnClickListener{
    private lateinit var binding: ActivityGuideBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 隐藏状态栏
        supportActionBar?.hide()
        binding = ActivityGuideBinding.inflate(layoutInflater)
        setContentView(binding.root)

        // 设置第一个圆点为true
        getPosition(0)

        initView()
        initData()
    }

    private fun initData() {
        val reader = getSharedPreferences("isToken", Context.MODE_PRIVATE)
        // 判断是否第一次登录
        if(reader.getBoolean("true", false)) startActivity(Intent(this, MainActivity::class.java)).apply { finish() }
    }


    /**
     * 用户点击按钮进入软件
     */
    override fun onClick(p0: View?) {
        getSharedPreferences("isToken", Context.MODE_PRIVATE).edit().apply {
            putBoolean("true", true)
            // 提交
            apply()
        }
        // 进入软件主页
        startActivity(Intent(this, MainActivity::class.java))
        // 销毁布局
        finish()
    }


    private fun initView() {
        val layout = ArrayList<Int>().apply {
            this.add(R.layout.guide_1)
            this.add(R.layout.guide_2)
            this.add(R.layout.guide_3)
            this.add(R.layout.guide_4)
            this.add(R.layout.guide_5)
        }

        // 设置适配器
        binding.viewPager.adapter = ViewAdapter(layout)

        /**
         *
         */
        binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                getPosition(position)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })
    }

    /**
     * ViewPager适配器
     */
    inner class ViewAdapter(
        val layout: List<Int>
    ) : PagerAdapter(){
        override fun getCount() = layout.size

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view == `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = layoutInflater.inflate(layout[position], container, false)
            container.addView(view)
            return view
        }


        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            return container.removeView(`object` as View)
        }
    }


    /**
     * 指示器
     */
    fun getPosition(typePosition: Int){
        if (typePosition == 0) findViewById<ImageView>(R.id.guideImageView1).setImageResource(R.drawable.guide_ok)
        else findViewById<ImageView>(R.id.guideImageView1).setImageResource(R.drawable.guide_no)

        if (typePosition == 1) findViewById<ImageView>(R.id.guideImageView2).setImageResource(R.drawable.guide_ok)
        else findViewById<ImageView>(R.id.guideImageView2).setImageResource(R.drawable.guide_no)

        if (typePosition == 2) findViewById<ImageView>(R.id.guideImageView3).setImageResource(R.drawable.guide_ok)
        else findViewById<ImageView>(R.id.guideImageView3).setImageResource(R.drawable.guide_no)

        if (typePosition == 3) findViewById<ImageView>(R.id.guideImageView4).setImageResource(R.drawable.guide_ok)
        else findViewById<ImageView>(R.id.guideImageView4).setImageResource(R.drawable.guide_no)

        if (typePosition == 4) findViewById<ImageView>(R.id.guideImageView5).setImageResource(R.drawable.guide_ok)
        else findViewById<ImageView>(R.id.guideImageView5).setImageResource(R.drawable.guide_no)
    }
}